<?php
/**
 * ***********************************************************************************
 * 咨询鲤 PC端知识店铺-商品相关
 * 
 * @author Mr.RJH
 * @date 2025-09-16
 * @email<15213026209@163.com>
 * ***********************************************************************************
 */
namespace app\service;

use app\model\product\ZxlKnowStoreProductModel;
use app\model\product\ZxlKnowStoreCategoryModel;

class ProductService
{ 
    /**
     * 商户ID
     * @var int
     */
    protected $m_id;

    /**
     * 构造方法
     * KuaishouWebService constructor.
     */
    public function __construct($m_id='')
    {
        $this->m_id = $m_id;
        return $this;
    }

    /**
     * 根据产品类型获取推荐商品
     * 
     * @param int $product_type 产品类型
     * @param int limit 推荐数量
     * @return array
     */
    public function recommend_product($product_type, $limit)
    {
        //查询条件
        $where = function($q)use($product_type){
            $q->where('m_id', $this->m_id);
            $q->where('product_type', $product_type);
            $q->where('is_delete', 0);
            $q->where('status', 1);
            $q->where('visible_range', 0);
        };
        $query = ZxlKnowStoreProductModel::with("attrMinPrice")->where($where);
        $list = $query->field('id,name,product_type,sub_title,head_imgs,real_sales,virtual_sales,tags,max_users')->order('real_sales', 'desc')->limit($limit)->select()->toArray();
        foreach ($list as &$v) {
            $v['product_type_text'] = ZxlKnowStoreProductModel::getPcProductType($v['product_type']);
            $v['head_imgs'] = $v['head_imgs'] ? explode(',',$v['head_imgs'])[0] : '/static/images/no_pic_1.png';
            $v['tags'] = ZxlKnowStoreProductModel::get_tag_list($v['tags']);
            $v['max_users'] = ZxlKnowStoreProductModel::get_max_users($v['max_users']);
            $v['href'] = ZxlKnowStoreProductModel::getPcProductDetailHref($v['product_type'], $v['id']);
        }
        
        return $list;
    }

    /**
     * 随机推荐商品
     * 
     * @return int $limit 推荐数量 
     */
    public function rand_recommend_product($limit, $product_type='')
    {
        if ($product_type == '') {
            $product_type = ZxlKnowStoreProductModel::PC_PRODUCTTYPE;
        }
        //查询条件
        $where = function($q)use($product_type){
            $q->where('m_id', $this->m_id);
            $q->where('is_delete', 0);
            $q->whereIn('product_type', $product_type);
            $q->where('status', 1);
            $q->where('visible_range', 0);
        };
        $query = ZxlKnowStoreProductModel::with("attrMinPrice")->where($where);
        $list = $query->field('id,name,product_type,sub_title,background_color,head_imgs,real_sales,virtual_sales,tags,max_users')->orderRaw('rand()')->limit($limit)->select()->toArray();
        foreach ($list as &$v) {
            $v['product_type_text'] = ZxlKnowStoreProductModel::getPcProductType($v['product_type']);
            if ($v['product_type'] == 10) {
                $v['head_imgs'] = '/static/images/exam.png';
            } else {
                $v['head_imgs'] = $v['head_imgs'] ? explode(',',$v['head_imgs'])[0] : '/static/images/no_pic_1.png';
            }
            $v['tags'] = ZxlKnowStoreProductModel::get_tag_list($v['tags']);
            $v['max_users'] = ZxlKnowStoreProductModel::get_max_users($v['max_users']);
            $v['href'] = ZxlKnowStoreProductModel::getPcProductDetailHref($v['product_type'], $v['id']);
        }
        
        return $list;
    }

    /**
     * 根据类型 获取商品列表
     * 
     * @param int $product_type 产品类型
     * @return array
     */
    public function get_product_list($product_type, $limit = 10, $page = 1, $cate_id='', $cate_level = 2)
    {
        $second_cate_ids = '';
        if ($cate_level == 1) {
            // 一级分类处理
            $second_cate_ids = ZxlKnowStoreCategoryModel::where(['m_id' => $this->m_id, 'parent_id' => $cate_id, 'status' => 1, 'is_delete' => 0])->column('id');
        }
        //查询条件
        $where = function($q)use($product_type,$cate_level,$cate_id,$second_cate_ids){
            $q->where('m_id', $this->m_id);
            $q->where('product_type', $product_type);
            $q->where('is_delete', 0);
            $q->where('status', 1);
            $q->where('visible_range', 0);
            if ($cate_id != '' && $cate_id > 0) {
                if ($cate_level == 2) {
                    $q->wherefindinset('cate_id', $cate_id);
                } else {
                    $q->where(function($query) use ($second_cate_ids) {
                        foreach ($second_cate_ids as $cateId) {
                            $query->whereOrRaw("FIND_IN_SET('{$cateId}', cate_id)");
                        }
                    });
                }
            }
        };
        $query = ZxlKnowStoreProductModel::with("attrMinPrice")->where($where);
        
        $list = $query->field('id,p_product_id,name,sub_title,head_imgs,real_sales,virtual_sales,tags')->order(['sort' => 'desc','create_time' => 'desc'])->paginate($limit,false,['page'=>$page])->toArray();
        return $list;
    }
}